const path = require('path');
const CompressionPlugin = require('compression-webpack-plugin'); // 引入gzip压缩插件

const autoprefixer = require('autoprefixer');
const pxtoviewport = require('postcss-px-to-viewport');

const publicPath = process.env.VUE_APP_PUBLIC_PATH;
const devPort = process.env.VUE_APP_DEV_PORT || undefined;
const isProd = process.env.NODE_ENV === 'production';
const isDebug = process.env.VUE_APP_Debug === '1' || process.env.VUE_APP_VConsole === '1';;

module.exports = {
  devServer: {
    host: '0.0.0.0',
    port: devPort,
    proxy: {
      '/front/': {
        target: 'http://wx-inside.szxxkjtech.com',
        // target: 'http://test.szxxkjtech.com',
        secure: false,
        ws: true,
        changOrigin: true,
        pathRewrite: {
          // '^/front/': '/',
          '^/front/': '/front/',
        },
      },
    },
  },
  publicPath: publicPath,
  productionSourceMap: false,
  runtimeCompiler: false,
  // parallel: require('os').cpus().length > 1,
  // BUG: https://github.com/vuejs/vue-loader/issues/1747
  parallel: false,
  css: {
    requireModuleExtension: true,
    extract: isProd && !isDebug,
    sourceMap: !isProd || isDebug,
    loaderOptions: {
      less: {
        modifyVars: {
          hack: `true; @import "~@/assets/less/theme.less";`,
        },
      },
      postcss: {
        plugins: [
          autoprefixer(),
          pxtoviewport({
            viewportWidth: 375,
          }),
        ],
      },
    },
  },
  chainWebpack: (config) => {
    // 配置别名
    config.resolve.alias.set('@', resolve('src'));
    config.when(isProd && !isDebug, (config) => {
      // 去除生产环境console, debugger
      config.optimization.minimizer('terser').tap((options) => {
        options.forEach((opt) =>
          Object.assign(opt.terserOptions.compress, {
            // drop_debugger: true,
            // drop_console: true,
            // pure_funcs: ['console.log'],
          })
        );
        return options;
      });
      // 压缩html中style标签
      config.plugin('html').tap((options) => {
        options.forEach((opt) =>
          Object.assign(opt.minify, {
            minifyCSS: true,
          })
        );
        return options;
      });
    });
  },
  configureWebpack: (config) => {
    config.externals = {
      wx: 'wx',
      VConsole: 'VConsole',
    };
    if (isProd && !isDebug) {
      // gzip压缩配置
      config.plugins.push(
        new CompressionPlugin({
          algorithm: 'gzip',
          compressionOptions: { level: 9 },
          filename: '[path][base].gz',
          minRatio: 0.8,
          threshold: 1024,
          test: /\.(js|json|css|html|svg|woff|ttf|eot|otf)$/,
        })
      );
      // brotli压缩配置
      config.plugins.push(
        new CompressionPlugin({
          algorithm: 'brotliCompress',
          compressionOptions: { level: 11 },
          filename: '[path][base].br',
          minRatio: 0.8,
          threshold: 1024,
          test: /\.(js|json|css|html|svg|woff|ttf|eot|otf)$/,
        })
      );
    }
  },
};

function resolve(dir) {
  return path.join(__dirname, dir);
}
